/*

 Copyright 2012 Peter Dornbach.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
   
*/

package com.dornbachs.zebra.guitarear;

public class Util {
	public static final String join(String sep, int[] items) {
		StringBuffer result = new StringBuffer();
		if (items.length == 0) {
			return "";
		}
		result.append(Integer.toString(items[0]));
		for (int i = 1; i < items.length; ++i) {
			result.append(sep);
			result.append(Integer.toString(items[i]));
		}
		return result.toString();
	}
	
	public static final String concat(String orig, int n) {
		if (n <= 0) {
			return "";
		}
		if (n == 1) {
		  return orig;
		}
		StringBuilder b = new StringBuilder(orig.length() * n);
		for (int i = 0; i < n; ++i) {
			b.append(orig);
		}
		return b.toString();
	}
	
	public static final int random(int max) {
		return (int) (Math.random() * max);
	}
	
	public static final int greatestCommonDivisor(int a, int b) {
    // Euclid rules.
		if (b > a) {
			int c = b;
			b = a;
			a = c;
		}
		while (true) {
			int r = a % b;
			if (r == 0) {
				return b;
			}
			a = b;
			b = r;
		}
	}
	
	public static final int leastCommonMultiple(int a, int b) {
		return a * b / greatestCommonDivisor(a, b);
	}
	
	public static final int factorial(int n) {
		int result = 1;
		for (int i = 1; i <= n; ++i) {
			result *= i;
		}
		return result;
	}
	
	public static final int combination(int n, int k) {
		return factorial(n) / (factorial(k) * factorial(n - k));
	}
}